package text.editor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.text.JTextComponent;


@SuppressWarnings("serial")
public class TextEditor extends JFrame implements ActionListener {
	public static void main(String[] args) {
		new TextEditor().setVisible(true);
	}

	JTextComponent textField;
        
	public TextEditor() {
		super("DarkPad");
                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                }
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(400, 300));
		pack();
		setLocationRelativeTo(null);

		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");

		JMenuItem saveMenuItem = new JMenuItem("Save");
		saveMenuItem.addActionListener(this);
		fileMenu.add(saveMenuItem);

		JMenuItem exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);

		JMenu editMenu = new JMenu("Edit");

                JMenu changeFontFormatMenuItem = new JMenu("Font Style");
                JMenuItem boldMenuItem = new JMenuItem("Bold");
                boldMenuItem.addActionListener(this);
                changeFontFormatMenuItem.add(boldMenuItem);
                JMenuItem normalMenuItem = new JMenuItem("Normal");
                normalMenuItem.addActionListener(this);
                changeFontFormatMenuItem.add(normalMenuItem);
                
		JMenu changeFontColorMenuItem = new JMenu("Font Color");

		JMenuItem redMenuItem = new JMenuItem("Red");
		redMenuItem.addActionListener(this);
		changeFontColorMenuItem.add(redMenuItem);

		JMenuItem blueMenuItem = new JMenuItem("Blue");
		blueMenuItem.addActionListener(this);
		changeFontColorMenuItem.add(blueMenuItem);

		JMenuItem greenMenuItem = new JMenuItem("Green");
		greenMenuItem.addActionListener(this);
		changeFontColorMenuItem.add(greenMenuItem);
                
                JMenuItem whiteMenuItem = new JMenuItem("White");
                whiteMenuItem.addActionListener(this);
                changeFontColorMenuItem.add(whiteMenuItem);

		editMenu.add(changeFontColorMenuItem);
                editMenu.add(changeFontFormatMenuItem);

		JMenuItem clearMenuItem = new JMenuItem("Clear");
		clearMenuItem.addActionListener(this);
		editMenu.add(clearMenuItem);

		menuBar.add(fileMenu);
		menuBar.add(editMenu);

		setJMenuBar(menuBar);

		textField = new JTextArea();
                JScrollPane pScroll = new JScrollPane(textField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                textField.setBackground(Color.black);
                textField.setForeground(Color.white);
		JPanel panel = new JPanel(new BorderLayout());
                panel.add(pScroll, BorderLayout.CENTER);
		//panel.add(textField, BorderLayout.CENTER);
		setContentPane(panel);
	}

	protected void save() throws IOException {
		JFileChooser dialog = new JFileChooser();
		File file = new File("file.txt");
		dialog.setSelectedFile(file);
		if (dialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
			file = dialog.getSelectedFile();
			FileWriter writer = new FileWriter(file);
			writer.append(textField.getText());
			writer.close();
                        JOptionPane.showMessageDialog(null, "File has been saved.");
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		if ("Save".equals(action)) {

			try {
				save();
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null, "Could not save the file.");
			}

		} else if ("Exit".equals(action)) {

			System.exit(0);

                } else if("Bold".equals(action)) {
                        Font font = new Font("Verdana", Font.BOLD, 12);
                        textField.setFont(font);
                        
                } else if("Normal".equals(action)) {
                        Font font = new Font("Verdana", Font.PLAIN, 12);
                        textField.setFont(font);
                       
		} else if ("Red".equals(action)) {

			textField.setForeground(Color.red);
                        
		} else if ("Blue".equals(action)) {

			textField.setForeground(Color.blue);

		} else if ("Green".equals(action)) {

			textField.setForeground(Color.green);
                
                } else if("White".equals(action)) {
                        textField.setForeground(Color.white);
                        
		} else if ("Clear".equals(action)) {

			textField.setText(null);

		}
	}
}